package com.example.idachuappone.index.item;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.adapter.AdapterHotGreedImg;
import com.example.idachuappone.index.adapter.AdapterHotGreedName;
import com.example.idachuappone.index.dialog.IDialog;
import com.example.idachuappone.index.entity.HotService;
import com.example.idachuappone.index.entity.HotServiceItemDetail;
import com.example.idachuappone.index.ui.MyGridView;
import com.example.idachuappone.index.ui.MyListView;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class HotDetailGreedActivity extends BaseActivity implements OnClickListener {
	private AdapterHotGreedImg adapterHotGreedImg;
	private AdapterHotGreedName adapterHotGreedName;
	private Button btn_back;
	private Button btn_hot_phone;
	private Button btn_submit;
	private BitmapUtils bu;
	private Dialog dialogLoading;
	private HotService hotService;
	private HotServiceItemDetail hotServiceItemDetail;
	private String id;
	private ImageView img_head;
	private MyGridView mgv_greed_img;
	private MyListView mlv_main_greed;
	private TextView tv_name;
	private TextView tv_promp_warn;
	private TextView tv_service_flow;
	private TextView tv_slogan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hot_detail_greed);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		this.id = getIntent().getStringExtra("id");
		this.hotService = (HotService) getIntent().getSerializableExtra("hotService");
		initView();
		initData();
	}

	private void initData() {
		this.dialogLoading.show();
		NetUtil.get(this, new StringBuilder(Constant.HOTSERVICEINFODETAIL).append(this.id).toString(), 100,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						HotDetailGreedActivity.this.dialogLoading.dismiss();
						try {
							JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
							if (jsonObject.getInt("code") == 10000) {
								HotDetailGreedActivity.this.hotServiceItemDetail = new HotServiceItemDetail();
								HotDetailGreedActivity.this.hotServiceItemDetail.parseJson(jsonObject
										.getJSONObject("data"));
								HotDetailGreedActivity.this.setDataView();
								return;
							}
							MainToast.show(HotDetailGreedActivity.this, jsonObject.getString("error"), 0);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						HotDetailGreedActivity.this.dialogLoading.dismiss();
						MainToast.show(HotDetailGreedActivity.this, HotDetailGreedActivity.this.getResources()
								.getString(R.string.netFail), 0);
					}
				});
	}

	private void setDataView() {
		this.btn_back.setText(this.hotService.getName());
		this.tv_name.setText(this.hotServiceItemDetail.getName());
		this.tv_slogan.setText(this.hotServiceItemDetail.getSlogan());
		this.bu.display(this.img_head, this.hotServiceItemDetail.getHead());
		this.tv_promp_warn.setText(this.hotServiceItemDetail.getBespeak_info());
		initGreedNameAdapter();
		initGreedImgAdapter();
		String str = "";
		for (int i = 0; i < this.hotServiceItemDetail.getFlow().size(); i++) {
			str = new StringBuilder(String.valueOf(str)).append(i + 1).append("\u3001")
					.append(this.hotServiceItemDetail.getFlow().get(i)).toString();
			if (i != this.hotServiceItemDetail.getFlow().size() - 1) {
				str = new StringBuilder(String.valueOf(str)).append("\n").toString();
			}
		}
		this.tv_service_flow.setText(str);
	}

	private void initGreedImgAdapter() {
		this.adapterHotGreedImg.setList(this.hotServiceItemDetail.getImgs());
		this.adapterHotGreedImg.notifyDataSetChanged();
	}

	private void initGreedNameAdapter() {
		this.adapterHotGreedName.setList(this.hotServiceItemDetail.getRecipes());
		this.adapterHotGreedName.notifyDataSetChanged();
	}

	private void initView() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.btn_hot_phone = (Button) findViewById(R.id.btn_hot_phone);
		this.btn_hot_phone.setOnClickListener(this);
		this.mlv_main_greed = (MyListView) findViewById(R.id.mlv_main_greed);
		this.adapterHotGreedName = new AdapterHotGreedName(this);
		this.mlv_main_greed.setAdapter(this.adapterHotGreedName);
		this.mgv_greed_img = (MyGridView) findViewById(R.id.mgv_greed_img);
		this.adapterHotGreedImg = new AdapterHotGreedImg(this);
		this.mgv_greed_img.setAdapter(this.adapterHotGreedImg);
		this.tv_name = (TextView) findViewById(R.id.tv_name);
		this.tv_slogan = (TextView) findViewById(R.id.tv_slogan);
		this.img_head = (ImageView) findViewById(R.id.img_head);
		this.tv_service_flow = (TextView) findViewById(R.id.tv_service_flow);
		this.tv_promp_warn = (TextView) findViewById(R.id.tv_promp_warn);
		this.btn_submit = (Button) findViewById(R.id.btn_submit);
		this.btn_submit.setOnClickListener(this);
		String formatTime = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).format(new Date());
		if (ComUtil.getDateLong1(this.hotService.getFrom()) > ComUtil.getDateLong1(formatTime)) {
			this.btn_submit.setEnabled(false);
			this.btn_submit.setText("\u9884\u8ba2\u8fd8\u672a\u5f00\u59cb\uff0c\u656c\u8bf7\u5173\u6ce8");
		}
		if (ComUtil.getDateLong1(this.hotService.getTo()) < ComUtil.getDateLong1(formatTime)) {
			this.btn_submit.setEnabled(false);
			this.btn_submit.setText("\u4f60\u6765\u665a\u4e86\uff0c\u8ba2\u5355\u5df2\u6ee1");
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.btn_submit /* 2131165282 */:
			if (this.hotServiceItemDetail == null) {
				MainToast.show(this, "\u6570\u636e\u52a0\u8f7d\u5931\u8d25", 0);
			} else if (this.hotService.getDates() == null || this.hotService.getDates().size() == 0) {
				MainToast.show(this, "\u9884\u7ea6\u5df2\u6ee1\uff0c\u6b22\u8fce\u4e0b\u6b21\u9884\u7ea6", 0);
			} else {
				Intent intent = new Intent(this, HotPrompActivity.class);
				intent.putExtra("hotServiceItemDetail", this.hotServiceItemDetail);
				intent.putExtra("dates", (Serializable) this.hotService.getDates());
				startActivity(intent);
			}
		case R.id.btn_hot_phone /* 2131165306 */:
			new IDialog().showPhone(this).show();
		default:
		}
	}
}
